package xmlsync2;
import java.sql.CallableStatement;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.log4j.Logger;

import datawave.util.LoggerCache;

/**
 * @author jinwoo
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class MetaParameter
{
	private String name;
	private short columntype;
	private short datatype;
	private short nullable;
	private String typename;
	private Object value = null;
	private String encoding = null;
	private static Logger log = LoggerCache.get(MetaParameter.class.getName());

	/**
	 * Constructor Parameter.
	 * @param string
	 * @param s
	 * @param s1
	 */
	public MetaParameter(final ResultSet parameter, final String encoding) throws SQLException
	{
		this.encoding = encoding;
		this.name = parameter.getString("COLUMN_NAME");
		if (this.name == null)
			this.name = parameter.getString("PROCEDURE_NAME");
		this.columntype = parameter.getShort("COLUMN_TYPE");
		this.datatype = parameter.getShort("DATA_TYPE");
		this.nullable = parameter.getShort("NULLABLE");
		this.typename = parameter.getString("TYPE_NAME");
	}
	/**
	 * Method getdatatype.
	 */
	public short getdatatype()
	{
		return datatype;
	}

	/**
	 * Method getcolumntype.
	 * @return int
	 */
	public int getcolumntype()
	{
		return columntype;
	}
	/**
	 * Method createparameter.
	 * @param statement
	 * @param i
	 * @return Parameter
	 * 0 : DatabaseMetaData.procedureColumnUnknown
	 * 1 : DatabaseMetaData.procedureColumnIn
	 * 2 : DatabaseMetaData.procedureColumnInOut
	 * 3 : DatabaseMetaData.procedureColumnResult
	 * 4 : DatabaseMetaData.procedureColumnOut
	 * 5 : DatabaseMetaData.procedureColumnReturn
	 */
	public Parameter createparameter(CallableStatement statement, int i) throws SQLException, XmlsyncException
	{
		Parameter parameter = new Parameter(name, columntype, datatype, typename, encoding);
		if (columntype != DatabaseMetaData.procedureColumnIn)
			parameter.set(getvalue(statement, i));
		return parameter;
	}

	/**
	 * Method getvalue.
	 * @param statement
	 * @param i
	 * @param datatype
	 * @param typename
	 */
	private Object getvalue(
		CallableStatement statement,
		final int i) throws SQLException, XmlsyncException
	{
		switch (datatype)
		{
			case Types.BIT:
				return (new Boolean(statement.getBoolean(i))).toString();
			case Types.DATE :
			case Types.TIME :
			case Types.TIMESTAMP :
				return Str.dateformat(statement.getDate(i));
			case Types.OTHER :
				if (typename.equalsIgnoreCase("REF CURSOR"))
					return new RecordSet((ResultSet)(statement.getObject(i)), encoding);
				else return statement.getString(i);
			default :
				return statement.getString(i);
		}
	}
	
	/**
	 * Method gettypename.
	 * @return String
	 */
	public String gettypename()
	{
		return typename;
	}

}
